package p_stack_queue;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 栈和队列课后习题
 * User: 王圆豪
 * Date: 2024-07-29
 * Time: 22:59
 */
public class Solution {
    //逆波兰表达式求值
    public static int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        for (String token : tokens) {
            if (!token.equals("+") && !token.equals("*") && !token.equals("-") && !token.equals("/")) {
                stack.push(token);
            } else {
                String s1 = stack.pop();
                String s2 = stack.pop();
                switch (token) {
                    case "+" ->
                            stack.push(String.valueOf(Integer.parseInt(s2) + Integer.parseInt(s1)));
                    case "-" ->
                            stack.push(String.valueOf(Integer.parseInt(s2) - Integer.parseInt(s1)));
                    case "*" ->
                            stack.push(String.valueOf(Integer.parseInt(s2) * Integer.parseInt(s1)));
                    case "/" ->
                            stack.push(String.valueOf(Integer.parseInt(s2) / Integer.parseInt(s1)));
                }
            }
        }
        return Integer.parseInt(stack.peek());
    }

    public static void main2(String[] args) {
        String[] string = new String[]{"2", "1", "+", "3", "*"};
        System.out.println(evalRPN(string));
    }
    //括号匹配问题
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char k = s.charAt(i);
            if (k == '{' || k == '(' || k == '['){
                stack.push(k);
            }else if (k == '}' || k == ')' || k == ']'){
                if (stack.isEmpty()) {
                    return false;
                }
                switch (k) {
                    case '}' -> {
                        if (stack.peek() != '{') {
                            return false;
                        }
                        stack.pop();
                    }
                    case ')' -> {
                        if (stack.peek() != '(') {
                            return false;
                        }
                        stack.pop();
                    }
                    case ']' -> {
                        if (stack.peek() != '[') {
                            return false;
                        }
                        stack.pop();
                    }
                }
            }
        }
        return !stack.isEmpty();
    }
    //栈的弹出压入序列
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int k : pushV) {
            stack.push(k);
            while (j < popV.length && !stack.isEmpty() && popV[j] == stack.peek()) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }

    public static void main1(String[] args) {
        int[] a1 = new int[]{2,1,0};
        int[] a2 = new int[]{1,2,0};
        System.out.println(IsPopOrder(a1, a2));
    }
}
